package com.approval.approvalapp.index.cygn.wzly;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.approval.approvalapp.R;
import com.approval.approvalapp.bean.WzCommitBean;
import com.approval.approvalapp.bean.WzlyListBean;
import com.approval.approvalapp.util.SpUtil;
import com.approval.approvalapp.util.base.BaseActivity;
import com.approval.approvalapp.util.base.ContantActivity;
import com.approval.approvalapp.util.net.NetRequsetUtil;
import com.approval.approvalapp.util.net.interfacepac.NetDownResponse;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

//物资领用清单
public class WzlyQdActivity extends BaseActivity implements WzlyQdAdapter.ClickRvListener {


    @Bind(R.id.back_iv)
    ImageView backIv;
    @Bind(R.id.titleTv1)
    TextView titleTv1;
    @Bind(R.id.rv)
    RecyclerView rv;
    @Bind(R.id.nr_tv)
    TextView nrTv;
    @Bind(R.id.num_tv)
    TextView numTv;
    @Bind(R.id.commit_iv)
    TextView commitIv;
    @Bind(R.id.rl)
    RelativeLayout rl;
    private List<WzlyListBean> list1;

    @Override
    public int getLayoutId() {
        return R.layout.activity_wzly_qd;
    }

    @Override
    public void initView() {
        list1 = (List<WzlyListBean>) getIntent().getSerializableExtra("list");
        int allcount = 0;
        for (int i = 0; i < list1.size(); i++) {
            allcount = allcount + list1.get(i).getRedcount();
        }
        numTv.setText(allcount + "");
        rv.setLayoutManager(new LinearLayoutManager(this));
        WzlyQdAdapter wzlyQdAdapter = new WzlyQdAdapter();
        wzlyQdAdapter.setList1(list1 , WzlyQdActivity.this);
        rv.setAdapter(wzlyQdAdapter);
    }

    @Override
    public void initData() {

    }


    String nrstr;

    @OnClick({R.id.back_iv, R.id.commit_iv, R.id.rl})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_iv:
                finish();
                break;
            case R.id.commit_iv:
                if (TextUtils.isEmpty(nrstr)){
                    Toast.makeText(this, "请输入用途", Toast.LENGTH_SHORT).show();
                    return;
                }
                JSONObject jsonObject = new JSONObject();
                List<WzCommitBean> wzCommitBeans = new ArrayList<>();
                for (int i = 0; i <list1.size() ; i++) {
                    for (int j = 0; j <list1.get(i).getList().size() ; j++) {
                        wzCommitBeans.add(new WzCommitBean(list1.get(i).getList().get(j).getId(),
                                list1.get(i).getList().size()+"",
                                list1.get(i).getList().get(j).getName()));
                    }
                }
                jsonObject.put("array",wzCommitBeans);
                jsonObject.put("content",nrstr);
                jsonObject.put("org_code",SpUtil.getString(this,SpUtil.ORG_CODE,""));
                jsonObject.put("dan_wei",SpUtil.getString(this,SpUtil.UNIT_NAME,""));
                jsonObject.put("name", SpUtil.getString(this,SpUtil.USER_RUN_NICKNAME,""));
                Log.d("WzlyQdActivitsssy", jsonObject.toString());
                NetRequsetUtil.getInstance().netRequestPostJson("v1/center/material_add_function", jsonObject.toString(), new NetDownResponse() {
                    @Override
                    public void success(String str) {
                        Toast.makeText(WzlyQdActivity.this, "提交成功", Toast.LENGTH_SHORT).show();
                        WzlyQdActivity.this.finish();
                    }

                    @Override
                    public void errowithresponse(String str) {

                    }

                    @Override
                    public void erro() {

                    }

                    @Override
                    public void finish() {

                    }
                });
                break;
            case R.id.rl:
                ContantActivity.goInto(this, 1, "用途说明", nrstr);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 1:
                if (data != null) {
                    nrstr = data.getStringExtra(ContantActivity.returnkey);
                    nrTv.setText(nrstr);
                }
                break;
        }
    }

    @Override
    public void listener(String id) {
        WzlyActivity.instance.refresh(id);
        WzlyQdActivity.this.finish();


    }
}
